﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Scriptor.Presentation.Entity;

namespace Scriptor.Presentation.Templates
{
    public class CommunicationTemplate
    {
        List<string> gUsing { get; set; }
        public StringBuilder clausulaUsingFormatada { get; set; }

        List<string> gCommunication { get; set; }
        public StringBuilder clausulaCommunicationFormatada { get; set; }

        List<string> gMethod { get; set; }
        public StringBuilder methodCommunicationFormatado { get; set; }

        public string strUsing()
        {
            gUsing = new List<string>();

            gUsing.Add("using System;");
            gUsing.Add("using System.ComponentModel;");
            gUsing.Add("using Sinacor.Infra.UI.Common;");
            gUsing.Add("using System.ServiceModel;");
            gUsing.Add("using Sinacor.Infra.UI.Security.Services;");
            gUsing.Add("using System.Collections.Generic;");
            
            clausulaUsingFormatada = new StringBuilder();

            foreach (string str in gUsing)
            {
                clausulaUsingFormatada.Append(str);
                clausulaUsingFormatada.Append(Environment.NewLine);
            }

            return clausulaUsingFormatada.ToString();
        }

        public string strCommunication(string pNameSpace, string pCommunicationName, bool showInheriting)
        {
            gCommunication = new List<string>();

            gCommunication.Add("namespace " + pNameSpace);
            gCommunication.Add("{");
            
            if(showInheriting)
                gCommunication.Add("\tpublic class "+ pCommunicationName + " : " + "BaseCommunication");
            else
                gCommunication.Add("\tpublic class " + pCommunicationName);

            gCommunication.Add("\t{");
            gCommunication.Add("@@communication-body");
            gCommunication.Add("\t}");
            gCommunication.Add("}");

            clausulaCommunicationFormatada = new StringBuilder();

            foreach (string str in gCommunication)
            {
                clausulaCommunicationFormatada.Append(str);
                clausulaCommunicationFormatada.Append(Environment.NewLine);
            }

            return clausulaCommunicationFormatada.ToString();
        }

        public string strMethod(Method mtd)
        {
            string retorno = "void";

            if (!string.IsNullOrEmpty(mtd.ReturnName))
            {
                if (mtd.ReturnIsList)
                    retorno = string.Format("ModelCollection<Model.{0}>", mtd.ReturnName);
                else
                    retorno = string.Format("Model.{0}", mtd.ReturnName);
            }

            string parameter = string.Empty;

            foreach (Param param in mtd.ParamList)
            {
                if (param.ParameterIsList)
                {
                    if (param.ParameterIsParameter)
                        parameter += string.Format("ModelCollection<Model.Parameter.{0}>", param.ParameterType) + " " + param.ParameterName.ToLower();
                    else
                        parameter += string.Format("ModelCollection<Model.{0}>", param.ParameterType) + " " + param.ParameterName.ToLower();
                }
                else
                {
                    if (param.ParameterIsParameter)
                        parameter += string.Format("Model.Parameter.{0}", param.ParameterType) + " " + param.ParameterName.ToLower();
                    else
                        parameter += string.Format("Model.{0}", param.ParameterType) + " " + param.ParameterName.ToLower();
                }
                
                parameter += " , ";
            }

            if(!string.IsNullOrEmpty(parameter))
                parameter = parameter.Substring(0, parameter.Length - 2);          


            string method = "\t\tpublic " + retorno + " " + mtd.MethodName + " (" + parameter.Trim() + ")";

            methodCommunicationFormatado = new StringBuilder();
            //Adicionado: 21/8
            if (mtd.MethodComent.Trim().Length > 0)
            {
                methodCommunicationFormatado.Append("\t\t/// <summary>" + Environment.NewLine);
                methodCommunicationFormatado.Append("\t\t///" + mtd.MethodComent.Replace("\n", "\n\t\t///") + Environment.NewLine);
                methodCommunicationFormatado.Append("\t\t/// </summary>" + Environment.NewLine);
            }
            //Fim Adicionado. phsilva
            methodCommunicationFormatado.Append(method);
            methodCommunicationFormatado.Append(Environment.NewLine);
            methodCommunicationFormatado.Append("\t\t{");
            methodCommunicationFormatado.Append(Environment.NewLine);
            methodCommunicationFormatado.Append("\t\t}");
            methodCommunicationFormatado.Append(Environment.NewLine);
            methodCommunicationFormatado.Append(Environment.NewLine);

            return methodCommunicationFormatado.ToString();
        }

        public string AppendMethod(string pCommunicationScope, StringBuilder stb)
        {
            return pCommunicationScope.ToString().Replace("@@communication-body", stb.ToString());
        }       
    }
}
